package com.dkd.manage.service.impl;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.IdUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.domain.Vo.VendingMachineVo;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVmTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author dcg
 * @date 2025-07-15
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private INodeService nodeService;

    @Autowired
    private IVmTypeService vmTypeService;

    @Autowired
    private IChannelService channelService;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        // 新增设备
        String innerCode = UUIDUtils.getUUID();
        // 编码
        vendingMachine.setInnerCode(innerCode);
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node,vendingMachine,"id");
        // 地址额外处理
        vendingMachine.setAddr(node.getDetailedAddress());
        // 商圈也是
        vendingMachine.setBusinessType(node.getBusinessDistrictType());
        // 状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        int result =vendingMachineMapper.insertVendingMachine(vendingMachine);
        // 新增货道
        ArrayList<Channel> channels = new ArrayList<>();
        for (int i = 1; i <=vmType.getVmRow(); i++) {
            for (int j = 1; j <=vmType.getVmCol(); j++){
                // 创建货道
                Channel channel = new Channel();
                channel.setVmId(vendingMachine.getId());//// 货道id
                channel.setChannelCode(i+"-"+j);// 货道编号
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());// 货道最大容量
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channels.add(channel);
            }
        }
        channelService.batchInsertChannel(channels);
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if (vendingMachine.getNodeId() != null) {
            // 补充点位的信息，保持数据的一致性问题
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtils.copyProperties(node, vendingMachine, "id");// 把对应的字段拷贝过去，而两个表的id都一样，所以忽略id

            // 单独处理地址
            vendingMachine.setAddr(node.getDetailedAddress());
            // 商圈也是
            vendingMachine.setBusinessType(node.getBusinessDistrictType());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());// 更新时间
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
    /**
     * 查询设备管理列表
     *
     * @param vendingMachine 设备管理
     * @return 设备管理集合+合作商
     */
    public List<VendingMachineVo> selectVendingMachineList1(VendingMachine vendingMachine){
        return vendingMachineMapper.selectVendingMachineList1(vendingMachine);
    }


    /**
     * 根据设备id获取设备信息
     *
     * @param innerCode 设备管理
     * @return VendingMachine 设备信息
     */@Override
    public VendingMachine selectByInnerCode(String innerCode) {
        return vendingMachineMapper.selectByInnerCode(innerCode);
    }
}
